<template>
  <div class="mainlist-page">
    <!-- 返回按钮 -->
    <div class="header">
      <button class="back-btn" @click="goBack">← 返回</button>
      <div class="header-title">{{ mainListMeta.chapter_name || '主线任务' }}</div>
    </div>

    <!-- 章节故事（可折叠） -->
    <div class="opening" v-if="mainListMeta.opening_story">
      <div class="opening-content">{{ mainListMeta.opening_story }}</div>
    </div>

    <!-- 任务列表 -->
    <div class="list-container" v-if="!listLoading">
      <div v-for="(ch, idx) in mainQuestList" :key="idx" class="quest-card">
        <div class="card-header">
          <div class="area-info">
            <span class="area-name">{{ ch.area }}</span>
            <span v-if="ch.is_tg" class="tag recommend">推荐</span>
          </div>
          <div class="unlock-status" :class="{ locked: !ch.is_unlocked }">
            {{ ch.is_unlocked ? '✓ 已解锁' : '🔒 未解锁' }}
          </div>
        </div>

        <div class="card-body">
          <div class="quest-desc" v-if="ch.area_desc">{{ ch.area_desc }}</div>
          
          <div class="quest-info">
            <div class="info-row">
              <span class="info-label">等级：</span>
              <span class="info-value">{{ ch.min_level }} - {{ ch.max_level }}</span>
            </div>
            <div class="info-row" v-if="ch.cur_level">
              <span class="info-label">进度：</span>
              <span class="info-value">第 {{ ch.cur_level }} 关</span>
            </div>
          </div>

          <div class="boss-info" v-if="ch.boss_name">
            <span class="boss-label">BOSS：</span>
            <span class="boss-name">{{ ch.boss_name }}</span>
          </div>

          <div class="rewards-info" v-if="ch.rewards && ch.rewards.length">
            <div class="rewards-list">
              <div v-for="(rw, i) in ch.rewards.slice(0, 3)" :key="i" class="reward-tag">
                {{ rw.name || rw.item_name }}
                <span v-if="rw.count || rw.amount">×{{ rw.count || rw.amount }}</span>
              </div>
            </div>
          </div>
        </div>

        <div class="card-action">
          <button class="challenge-btn" @click="enterMainQuestFromList(ch)" :disabled="!ch.is_unlocked">
            {{ ch.is_unlocked ? '进入挑战' : '未解锁' }}
          </button>
        </div>
      </div>
    </div>

    <div class="loading" v-if="listLoading">
      <div class="loading-spinner"></div>
      <div class="loading-text">加载中...</div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AdventureMainList',
  data() {
    return {
      listLoading: false,
      mainListMeta: {
        chapter_name: '',
        opening_story: ''
      },
      mainQuestList: []
    }
  },
  mounted() {
    this.fetchMainList()
  },
  methods: {
    async fetchMainList() {
      this.listLoading = true
      try {
        const api = (await import('@/api')).default
        const res = await api.adventure.getMainList('main')
        const { opening_story, chapter_name, chapters } = res.data
        this.mainListMeta = {
          chapter_name: chapter_name || '主线任务',
          opening_story: opening_story || ''
        }
        this.mainQuestList = (chapters || []).map(ch => ({
          chapter_name: chapter_name || '',
          area: ch.area,
          area_desc: ch.area_desc,
          cur_level: ch.cur_level,
          is_unlocked: !!ch.is_unlocked,
          is_tg: !!ch.is_tg,
          min_level: ch.min_level,
          max_level: ch.max_level,
          boss_name: ch.boss_name,
          boss_desc: ch.boss_desc,
          rewards: ch.rewards || [],
          boss_id: ch.boss_id,
          enemy_id: ch.enemy_id,
          area_id: ch.area_id,
          area_key: ch.area_key
        }))
      } finally {
        this.listLoading = false
      }
    },
    goBack() {
      if (typeof uni !== 'undefined' && uni.navigateBack) {
        uni.navigateBack()
      } else {
        window.history.back()
      }
    },
    enterMainQuestFromList(chapter) {
      if (!chapter || !chapter.is_unlocked) {
        if (typeof uni !== 'undefined' && uni.showToast) {
          uni.showToast({ title: '未解锁，无法挑战', icon: 'none' })
        }
        return
      }
      const battleType = 'pve'
      const fallback = `monster_${Math.max(1, parseInt(chapter.cur_level || 1))}`
      const targetId = chapter.boss_id || chapter.enemy_id || chapter.area_id || chapter.area_key || chapter.boss_name || fallback
      const url = `/pages/battle/index?battleType=${encodeURIComponent(battleType)}&targetId=${encodeURIComponent(targetId)}`
      if (typeof uni !== 'undefined' && uni.navigateTo) {
        uni.navigateTo({ url })
      } else {
        window.location.hash = url
      }
    }
  }
}
</script>

<style scoped>
/* 页面容器 - 武侠风格 */
.mainlist-page {
  min-height: 100vh;
  max-height: 100vh;
  height: 100vh;
  height: calc(100vh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  overflow: hidden;
  background: linear-gradient(180deg, #e8dcc4 0%, #d4c4a8 100%);
  display: flex;
  flex-direction: column;
  font-family: "STSong", "Noto Serif SC", serif;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

/* 头部 */
.header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 15px;
  background: linear-gradient(135deg, #8b6f47 0%, #6b4f3b 100%);
  border-bottom: 3px solid #4b3a2a;
  box-shadow: 0 4px 12px rgba(139, 111, 71, 0.3);
  flex-shrink: 0;
}

.back-btn {
  padding: 6px 12px;
  background: rgba(255, 255, 255, 0.2);
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 8px;
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
}

.header-title {
  flex: 1;
  font-size: 18px;
  font-weight: bold;
  color: #fff;
  letter-spacing: 2px;
  text-shadow: 2rpx 2rpx 4rpx rgba(0, 0, 0, 0.3);
}

/* 章节故事 */
.opening {
  padding: 10px 15px;
  background: rgba(255, 255, 255, 0.6);
  border-bottom: 2px solid #d8c7a3;
  flex-shrink: 0;
}

.opening-content {
  font-size: 12px;
  color: #6b4f3b;
  line-height: 1.5;
  text-align: center;
  font-style: italic;
}

/* 列表容器 - 确保可滚动但不超出屏幕 */
.list-container {
  flex: 1;
  overflow-y: auto;
  padding: 10px 12px;
  padding-bottom: calc(15px + 80px + env(safe-area-inset-bottom)); /* TabBar高度 + 安全区域 */
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* 任务卡片 - 紧凑布局 */
.quest-card {
  background: rgba(255, 255, 255, 0.95);
  border: 2px solid #c4a57b;
  border-radius: 12px;
  padding: 10px 12px;
  box-shadow: 0 3px 10px rgba(139, 111, 71, 0.2);
  transition: all 0.3s;
}

.quest-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(139, 111, 71, 0.3);
  border-color: #8b6f47;
}

/* 卡片头部 */
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 1px solid #d8c7a3;
}

.area-info {
  display: flex;
  align-items: center;
  gap: 6px;
}

.area-name {
  font-size: 15px;
  font-weight: bold;
  color: #4a3c2a;
  letter-spacing: 1px;
}

.tag {
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
}

.tag.recommend {
  background: linear-gradient(135deg, #ffd700 0%, #ffed4e 100%);
  color: #8a6a3f;
  border: 1px solid #d4af37;
}

.unlock-status {
  font-size: 11px;
  font-weight: 600;
  color: #2b8a3e;
  padding: 3px 8px;
  background: rgba(43, 138, 62, 0.1);
  border-radius: 8px;
  border: 1px solid rgba(43, 138, 62, 0.3);
}

.unlock-status.locked {
  color: #a61e4d;
  background: rgba(166, 30, 77, 0.1);
  border-color: rgba(166, 30, 77, 0.3);
}

/* 卡片主体 */
.card-body {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 8px;
}

.quest-desc {
  font-size: 12px;
  color: #6b4f3b;
  line-height: 1.4;
  padding: 6px 8px;
  background: rgba(139, 111, 71, 0.05);
  border-radius: 6px;
  border-left: 2px solid #8b6f47;
}

.quest-info {
  display: flex;
  gap: 12px;
  font-size: 11px;
}

.info-row {
  display: flex;
  align-items: center;
  gap: 4px;
}

.info-label {
  color: #8b6f47;
  font-weight: 500;
}

.info-value {
  color: #4a3c2a;
  font-weight: 600;
}

.boss-info {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  padding: 5px 8px;
  background: rgba(139, 111, 71, 0.08);
  border-radius: 6px;
}

.boss-label {
  color: #8b6f47;
  font-weight: 600;
}

.boss-name {
  color: #4a3c2a;
  font-weight: bold;
}

.rewards-info {
  margin-top: 2px;
}

.rewards-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.reward-tag {
  font-size: 11px;
  color: #6b4f3b;
  background: rgba(139, 111, 71, 0.1);
  border: 1px solid #d8c7a3;
  border-radius: 12px;
  padding: 3px 8px;
  font-weight: 500;
}

/* 卡片操作 */
.card-action {
  display: flex;
  justify-content: center;
}

.challenge-btn {
  width: 100%;
  padding: 8px 16px;
  background: linear-gradient(135deg, #8b6f47 0%, #6b4f3b 100%);
  color: #fff;
  border: 2px solid #4b3a2a;
  border-radius: 10px;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s;
  letter-spacing: 1px;
  box-shadow: 0 3px 10px rgba(75, 58, 42, 0.25);
}

.challenge-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(75, 58, 42, 0.35);
}

.challenge-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  background: #d0d0d0;
  border-color: #999;
  color: #666;
  box-shadow: none;
}

/* 加载状态 */
.loading {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 25px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  border: 2px solid #8b6f47;
  box-shadow: 0 8px 24px rgba(139, 111, 71, 0.3);
}

.loading-spinner {
  width: 35px;
  height: 35px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #8b6f47;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  color: #4a3c2a;
  font-size: 14px;
  font-weight: 600;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .header {
    padding: 10px 12px;
  }
  
  .header-title {
    font-size: 16px;
  }
  
  .list-container {
    padding: 8px 10px 12px;
    gap: 8px;
  }
  
  .quest-card {
    padding: 8px 10px;
  }
  
  .area-name {
    font-size: 14px;
  }
  
  .quest-desc {
    font-size: 11px;
  }
  
  .challenge-btn {
    font-size: 13px;
    padding: 7px 14px;
  }
}
</style>
